package com.jiuyun.servlet;

import com.google.gson.Gson;
import com.jiuyun.dao.BookDao;
import com.jiuyun.dao.impi.BookDaoImpl;
import com.jiuyun.entity.Book;
import com.jiuyun.entity.Question;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/book/*")
public class BookServlet extends BaseServlet {
    BookDao bookDao = new BookDaoImpl();
    Gson gson = new Gson();

    protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        List<Book> list = null;
        try {
            list = bookDao.allbook();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //System.out.println(list);
        String j = gson.toJson(list);
        out.print(j);
        out.flush();
        out.close();


    }

    protected void book_in(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookname = request.getParameter("bookname");
        String p=request.getParameter("page");
        System.out.println("题目查询--"+bookname+"--"+p);
        int page=0;
        if(p==null) {
            page=1;
        }else {
            page=Integer.parseInt(p);
        }
        System.out.println(bookname);
        List<Question> list = null;
        int bookid = 0;
        int count=0;
        try {
            list = bookDao.alltimu(page,bookname);
            count=bookDao.count(bookname);
            bookid = bookDao.findbookid(bookname);
            request.setAttribute("count", count);
            request.setAttribute("bookid", bookid);
            request.setAttribute("bookname", bookname);
            request.setAttribute("timu", list);
            request.getRequestDispatcher("/book_lwj/book_in.jsp").forward(request, response);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }
    protected void book_updbook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //修改题库名称
        String bookname=request.getParameter("upd_bookname");
        int bookid=Integer.parseInt(request.getParameter("bookid"));
        System.out.println("修改题库名称"+bookname+bookid);
        try {
            int n=bookDao.upd_bookname(bookname,bookid);
            if(n>0){
                System.out.println("修改成功");
                response.sendRedirect("/Eventstar/book/book_in?bookname="+bookname);
            }else {
                System.out.println("修改失败");
                response.sendRedirect("/Eventstar/book/book_in?bookname="+bookname);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

    protected void book_add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookname = request.getParameter("new_bookname");
        String bookdesc = request.getParameter("new_bookdesc");
        Book book = new Book(0, bookname, bookdesc);
        System.out.println(bookname + "---" + bookdesc);
        try {
            int n = bookDao.addbook(book);
            if (n > 0) {
                System.out.println("添加成功");
                response.sendRedirect("/Eventstar/book_lwj/book.jsp");
            } else {
                System.out.println("添加失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

    protected void book_del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bid = request.getParameter("bookid");
        int bookid = Integer.parseInt(bid);
        System.out.println(bookid);
        try {
            int n = bookDao.delbook(bookid);
            if (n > 0) {
                System.out.println("删除成功");
                response.sendRedirect("/Eventstar/book_lwj/book.jsp");
            } else {
                System.out.println("删除失败");
                response.sendRedirect("/Eventstar/book_lwj/book.jsp");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    protected void book_deltimu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("删除题目");
        int qid=Integer.parseInt(request.getParameter("qid"));
        System.out.println(qid);
        try {
            int n=bookDao.deltimu(qid);
            if (n>0){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    protected void book_deltimus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookname=request.getParameter("bookname");
        System.out.println("删除多个题目--"+bookname);
        if (request.getParameterValues("inp")==null){
            response.sendRedirect("/Eventstar/book/book_in?bookname="+bookname);
        }else {
            String[] qids=request.getParameterValues("inp");
            int n=0;
            for (String q:qids){
                System.out.println(q);
                int qid=Integer.parseInt(q);
                try {
                    n=bookDao.deltimu(qid);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (n>0){
                System.out.println("删除成功");
                response.sendRedirect("/Eventstar/book/book_in?bookname="+bookname);

            }else {
                System.out.println("删除失败");
            }
        }


    }
    protected void book_addtimujsp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookname = request.getParameter("bookname");
        System.out.println("addtimu-"+bookname);
        List<Book> list = null;
        try {
            list = bookDao.allbook();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.setAttribute("allbook",list);
        request.setAttribute("bookname",bookname);
        request.getRequestDispatcher("/book_lwj/book_add.jsp").forward(request,response);

    }

    protected void book_addtimu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String btn=request.getParameter("btn");
        String bookname = request.getParameter("bookname");
        if(btn.equals("确定")) {
            System.out.println("添加题目");
            String qname = request.getParameter("qname");
            String qa = request.getParameter("qa");
            String qb = request.getParameter("qb");
            String qc = request.getParameter("qc");
            String qd = request.getParameter("qd");
            String ans = request.getParameter("daan");
            String bookname01 = request.getParameter("bookname01");
            int uid = 219;
            System.out.println(qname + "--" + qa + "--" + qb + "--" + qc + "--" + qd + "--" + bookname01+"---"+ans);
            if (qname.equals("") || qa.equals("") || qb.equals("") || qc.equals("")||ans.equals("")) {
                System.out.println("不能添加空数据");
                response.sendRedirect("/Eventstar/book/book_addtimujsp?bookname=" + bookname01);
            } else {
                int n = 0;
                try {
                    int bookid = bookDao.findbookid(bookname01);
                    Question q = new Question(0, qname, 0, uid, bookid, qa, qb, qc, qd, ans, null);
                    n = bookDao.addtimu(q);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                if (n > 0) {
                    System.out.println("添加成功");
                    response.sendRedirect("/Eventstar/book/book_addtimujsp?bookname="+bookname01);
                } else {
                    System.out.println("添加失败");
                }
            }

        }else if(btn.equals("取消")){
            System.out.println("取消添加--"+bookname);
            response.sendRedirect("/Eventstar/book/book_in?bookname=" + bookname);
        }
    }
    protected void book_updtimujsp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int qid=Integer.parseInt(request.getParameter("qid"));
        String bookname=request.getParameter("bookname");
        System.out.println(qid);
        List<Question> list = null;
        try {
            list=bookDao.findtimu(qid);
            System.out.println(list);
            request.setAttribute("bookname",bookname);
            request.setAttribute("updtimu",list);
            request.getRequestDispatcher("/book_lwj/book_upd.jsp").forward(request,response);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    protected void book_updtimu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String btn=request.getParameter("btn");
        String bookname = request.getParameter("bookname");
        if(btn.equals("确定")) {
            System.out.println("修改题目--"+bookname);
            int qid=Integer.parseInt(request.getParameter("qid"));
            String qname = request.getParameter("qname");
            String qa = request.getParameter("qa");
            String qb = request.getParameter("qb");
            String qc = request.getParameter("qc");
            String qd = request.getParameter("qd");
            String ans = request.getParameter("daan");
            int uid = 219;
            System.out.println(qname + "--" + qa + "--" + qb + "--" + qc + "--" + qd + "--"+qid+"--"+ans);
            if (qname.equals("") || qa.equals("") || qb.equals("") || qc.equals("")) {
                System.out.println("不能修改空数据");
                response.sendRedirect("/Eventstar/book/book_updtimujsp?bookname=" + bookname+"&qid="+qid);
            } else {
                int n = 0;
                try {
                    int bookid = bookDao.findbookid(bookname);
                    Question q=new Question(qid,qname,0,uid,bookid,qa,qb,qc,qd,ans,null);
                    n = bookDao.updtimu(q);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                if (n > 0) {
                    System.out.println("修改成功");
                    response.sendRedirect("/Eventstar/book/book_updtimujsp?bookname="+bookname+"&qid="+qid);
                } else {
                    System.out.println("修改失败");
                }
            }

        }else if(btn.equals("取消")){
            System.out.println("取消添加--"+bookname);
            response.sendRedirect("/Eventstar/book/book_in?bookname=" + bookname);
        }
    }
}